﻿using QxFramework.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using UnityEngine;
namespace QxFramework.Core
{

/// <summary>
/// 管理器基类，用于生成每个场景中特有的MonoSingleton来管理逻辑
/// </summary>
public abstract class ManagerBase<MT> : MonoSingleton<MT> where MT:ManagerBase<MT>
{
    /// <summary>
    /// 所有模块列表
    /// </summary>
    private readonly List<ModulePair> _modules = new List<ModulePair>();

    public static bool Enable;

    /// <summary>
    /// 初始化所有模块
    /// </summary>
    public virtual void InitModules()
    {
        _modules.Clear();
    //    Add<IMainDataManager>(new MainDataManager(), ModuleEnum.MainDataManager);
    }

    public static T Get<T>(string tag="")
    {
            
        var type = typeof(T);
            if (type.IsInterface == false)
            {
                Debug.LogError("请使用接口获取组件");
            }
        var pair = Instance._modules.Find((m) => (m.Module is T)&&tag==m.Module.Tag);
        if (pair == null)
        {
            Debug.Log("未注册的模块" + type.Name);

            return default(T);
        }
        if (pair.Initialized == false)
        {
            try
            {
                pair.Module.Awake();
                pair.Initialized = true;
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
        return (T)(object)pair.Module;
    }

    private void Awake()
    {
//        InitModules();
        Enable = true;
    }

    protected void Add<T>(LogicModuleBase module,object args=null)//, ModuleEnum moduleEnum)
    {
            _modules.Add(new ModulePair(typeof(T), module));//, moduleEnum));
            module.Init(args);
       /* try
        {
        }
        catch (Exception e)
        {
            Debug.LogError(e);
        }*/
    }

    private void Update()
    {
        for (int i = 0; i < _modules.Count; i++)
        {
            var module = _modules[i];
            try
            {
                module.Module.Update();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }

    private void FixedUpdate()
    {
        for (int i = 0; i < _modules.Count; i++)
        {
            var module = _modules[i];
            try
            {
                module.Module.FixedUpdate();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }

    private void LateUpdate()
    {
        for (int i = 0; i < _modules.Count; i++)
        {
            var module = _modules[i];
            try
            {
                module.Module.LateUpdate();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
    }
    private void OnDestroy()
    {
        for (int i = 0; i < _modules.Count; i++)
        {
            var module = _modules[i];
            try
            {
                module.Module.OnDestroy();
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        }
        MessageManager.Instance.RemoveAbout(this);
        Enable = false;
    }

    private class ModulePair
    {
        public readonly Type ModuleType;
        public readonly LogicModuleBase Module;
    //    public ModuleEnum TypeEnum;

        public bool Initialized;

        public ModulePair(Type moduleType, LogicModuleBase module)//, ModuleEnum typeEnum)
        {
            ModuleType = moduleType;
            Module = module;
        //    TypeEnum = typeEnum;
        }
    }
}

}
/*public enum ModuleEnum
{
    Unknow = 0,
    MainDataManager,
    Max,
}*/